# Paper: Is it still the economy? Economic voting in polarized politics
# Individual-level simulation
# Replication: Figure S14 (Supplementary Material)
remove(list = ls())

library(conflicted)
library(tidyverse)
library(dplyr)
conflict_prefer("filter","dplyr")
library(ggplot2)
library(here) # When we have an R-project, it reads files without setting an directory
conflict_prefer("here", "here")
library(nnet)
library(ggeffects)
library(MASS)
conflict_prefer("select","dplyr")


load(file = here("Final_Paper", "R&R", "CCES_PanelData_1014.RData"))


data_2014 <- data %>% filter(year == 2014)
summary(ml_inc14 <- MASS::polr(econ_eval2 ~ percep_polar_lag*partisan + 
                                 female + black + hispanic + asian + other_race + 
                                 log_age + college + income, 
                               data = data_2014, Hess=TRUE))



ml_inc2 <- ml_inc14 %>% 
  ggemmeans(terms = c("partisan", "percep_polar_lag[0.1, 0.5, 1]"), 
            ci.lvl = 0.95)
glimpse(ml_inc2)
ml_inc2 <- data.frame(ml_inc2)

ml_inc2 <- ml_inc2 %>% 
  rename(partisan = x)

ml_inc2 <- ml_inc2 %>% 
  mutate(econ_eva = ifelse(response.level == "1", "Worse", NA),
         econ_eva = ifelse(response.level == "2", "Same", econ_eva),
         econ_eva = ifelse(response.level == "3", "Better", econ_eva))

g_inc14 <- ml_inc2 %>% 
  mutate(partisan = factor(partisan,
                           levels = c("Dem", 
                                      "Ind",
                                      "Rep"),
                           labels = c("President's Party", 
                                      "Independent",
                                      "Opposing Party"))) %>% 
  mutate(econ_eva = factor(econ_eva,
                           levels = c("Worse", 
                                      "Same",
                                      "Better"),
                           labels = c("Worse", 
                                      "Same",
                                      "Better"))) %>% 
  mutate(group = as.factor(group)) %>% 
  #mutate(response.level = as.factor(response.level)) %>%
  filter(econ_eva == "Worse") %>% 
  #mutate(x = as.factor(x)) %>%
  #filter(response.level == "Inc") %>% 
  #filter(group == 0.33 | group == 1) %>% 
  ggplot(aes(shape = group)) + 
  #geom_hline(yintercept = 0, colour = "black", lty = 2, size = .75) +
  geom_pointrange(aes(x = partisan, y = predicted, 
                      ymin = conf.low, ymax = conf.high),
                  lwd = .85, position = position_dodge(width = .5),
                  color = "darkslategray") + 
  theme_light() + 
  labs(x = "Partisan Attachment", 
       y = "Probability of Negative Economic Evaluation",
       title = "(b) 2014 Wave") +
  #scale_color_manual(name = "Partisanship:", 
  #               values = c("gray17", "gray37", "gray57"),
  #                labels = c("Inpart", "Ind", "Outpart")) +
  scale_shape_manual(name = "Perceived Ideological Gap (2012):",
                     labels = c("0.1", "0.5", "1"),
                     values = c(15, 16, 17)) +
  #scale_fill_manual(name = "Partisanship:",
  #                 values = c("gray17", "gray37", "gray57"), 
  #                labels = c("Inpart", "Ind", "Outpart")) +
  scale_y_continuous(breaks = c(0.8, 0.6, 0.4, 0.2, 0),
                     limits = c(0, 0.85)) +
  theme(axis.text.x = element_text(hjust = 0.5, size = 12),
        axis.text.y = element_text(size = 10),
        axis.title.x = element_text(size = 12),
        axis.title.y = element_text(size = 12),
        legend.text = element_text(size = 12),
        legend.title = element_text(size = 12),
        legend.position = "bottom",
        legend.direction = "horizontal",
        plot.title = element_text(size = 14))
g_inc14

##################################################
##################################################
##################################################
########### 2012

data_2012 <- data %>% filter(year == 2012)
summary(ml_inc12 <- MASS::polr(econ_eval2 ~ percep_polar_lag*partisan + 
                                 female + black + hispanic + asian + other_race + 
                                 log_age + college + income, 
                               data = data_2012, Hess=TRUE))


ml_inc3 <- ml_inc12 %>% 
  ggemmeans(terms = c("partisan", "percep_polar_lag[0.1, 0.5, 1]"), 
            ci.lvl = 0.95)
glimpse(ml_inc3)
ml_inc3 <- data.frame(ml_inc3)

ml_inc3 <- ml_inc3 %>% 
  rename(partisan = x)

ml_inc3 <- ml_inc3 %>% 
  mutate(econ_eva = ifelse(response.level == "1", "Worse", NA),
         econ_eva = ifelse(response.level == "2", "Same", econ_eva),
         econ_eva = ifelse(response.level == "3", "Better", econ_eva))

g_inc12 <- ml_inc3 %>% 
  mutate(partisan = factor(partisan,
                           levels = c("Dem", 
                                      "Ind",
                                      "Rep"),
                           labels = c("President's Party", 
                                      "Independent",
                                      "Opposing Party"))) %>% 
  mutate(econ_eva = factor(econ_eva,
                           levels = c("Worse", 
                                      "Same",
                                      "Better"),
                           labels = c("Worse", 
                                      "Same",
                                      "Better"))) %>% 
  mutate(group = as.factor(group)) %>% 
  #mutate(response.level = as.factor(response.level)) %>%
  filter(econ_eva == "Worse") %>% 
  #mutate(x = as.factor(x)) %>%
  #filter(response.level == "Inc") %>% 
  #filter(group == 0.33 | group == 1) %>% 
  ggplot(aes(shape = group)) + 
  #geom_hline(yintercept = 0, colour = "black", lty = 2, size = .75) +
  geom_pointrange(aes(x = partisan, y = predicted, 
                      ymin = conf.low, ymax = conf.high),
                  lwd = .85, position = position_dodge(width = .5),
                  color = "darkslategray") + 
  theme_light() + 
  labs(x = "Partisan Attachment", 
       y = "Probability of Negative Economic Evaluation",
       title = "(a) 2012 Wave") +
  #scale_color_manual(name = "Partisanship:", 
  #               values = c("gray17", "gray37", "gray57"),
  #                labels = c("Inpart", "Ind", "Outpart")) +
  scale_shape_manual(name = "Perceived Ideological Gap (2010):",
                     labels = c("0.1", "0.5", "1"),
                     values = c(15, 16, 17)) +
  #scale_fill_manual(name = "Partisanship:",
  #                 values = c("gray17", "gray37", "gray57"), 
  #                labels = c("Inpart", "Ind", "Outpart")) +
  scale_y_continuous(breaks = c(0.8, 0.6, 0.4, 0.2, 0),
                     limits = c(0, 0.85)) +
  theme(axis.text.x = element_text(hjust = 0.5, size = 12),
        axis.text.y = element_text(size = 10),
        axis.title.x = element_text(size = 12),
        axis.title.y = element_text(size = 12),
        legend.text = element_text(size = 12),
        legend.title = element_text(size = 12),
        legend.position = "bottom",
        legend.direction = "horizontal",
        plot.title = element_text(size = 14))
g_inc12


multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)
  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)
  numPlots = length(plots)
  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                     ncol = cols, nrow = ceiling(numPlots/cols))
  }
  if (numPlots==1) {
    print(plots[[1]])
  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}

multiplot(g_inc12, g_inc14, cols = 2)


